import { createRouter, createWebHistory } from 'vue-router'
import { useCounterStore } from '../stores/counter'
import login from '@/views/login.vue'
import Regin from'../views/Regin.vue'
import Layout from '../views/Layout.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/',
      name :'login',
      component : login,
    },
    {
      path: '/regin',
      name: 'regin',
      component : Regin
    },
    {
      path: '/layout',
      name: 'layout',
      component: Layout
    }
  ],
})
router.beforeEach((to) => {
  const counterStore = useCounterStore()
   // 如果不是登录页或注册页，且用户未登录，则重定向到登录页
if(to.path !='/regin' && to.path !='/' && counterStore.count === ''){
  ElMessage.error('请先登录')
  return '/'
}}
)
export default router
